/**
 * test from osc@china
 * this is another test
 * this is the third update
 * this is the fourth update from remote
 * this is 5th
 * this is 6th
 * this is 7th
 */
package com.rtsportscast.netty.operator;

import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.sql.Timestamp;
import java.util.ArrayList;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

/**
 * Replayer.java <br>
 *
 * @author zhizhan
 * @since 2014-9-30
 */
public class Replayer implements Runnable {
	
	private Channel channel;
	
	private boolean isRunning = true;
	
	private String matchId;
	private String operatorId;
	private String token;
	

	public Replayer(){
	}
	public Replayer(Channel channel, String matchId, String operatorId, String token){
		this.channel = channel;
		this.matchId = matchId;
		this.operatorId = operatorId;
		this.token = token;
	}
	
	@Override
	public void run() {

		ArrayList<EventObj> list = this.getEventObjs(this.matchId);
		
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
		}
		
		int len = list.size();
		for(int i = 0; i < len ; i++){
			EventObj currentObj = (EventObj)list.get(i);
			String currntTime = currentObj.getTime();
			
			// send out the event
			StringBuffer sb = new StringBuffer(128);
			sb.append("logevent?userid=");sb.append(this.operatorId);
			sb.append("&check=null&message=&sent=&parent=&token=");sb.append(this.token);
			sb.append("&topic=");sb.append(this.matchId);
			sb.append("&eventtype=");sb.append(currentObj.getType());
			sb.append("&key=");sb.append(currentObj.getKey());
			System.out.println(sb.toString());
			this.channel.writeAndFlush(new TextWebSocketFrame(sb.toString()));
			
			
			if(i<len-1){
				int next = i + 1;
				EventObj nextObj = (EventObj)list.get(next);
				String nextTime = nextObj.getTime();
				Timestamp currentTimestamp = Timestamp.valueOf(currntTime.replace("/", "-"));
				Timestamp nextTimestamp = Timestamp.valueOf(nextTime.replace("/", "-"));
				long difference = nextTimestamp.getTime() - currentTimestamp.getTime() ;
				if(difference == 0 ) difference = 500;
				if(difference < 0)  difference = 0;
				// sleep for however long we need to
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {}
				
			}
		}
	}
	
	
	
	public static void main(String args[]){
		
		Replayer replayer = new Replayer();
		ArrayList<EventObj> list = replayer.getEventObjs("340755");
		for(EventObj eventObj: list){
			System.out.println(eventObj.getTime() +"    " + eventObj.getKey() + "   "+eventObj.getType());
			
		}
		
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			
		}
		
		Timestamp timestamp = Timestamp.valueOf("2007-9-3 01:10:10");
		System.out.println(timestamp.getTime());
		 timestamp = Timestamp.valueOf("2007-09-03 1:10:10");
		System.out.println(timestamp.getTime());
		
	}
	
	public ArrayList<EventObj> getEventObjs(String matchId){
		ArrayList<EventObj> list = new ArrayList<EventObj>();
		try {
			//Workbook workbook = Workbook.getWorkbook(new File("match1.xls"));
			//val file = new File(getClass.getResource("zipcode_data.csv").toURI)
			URL url = getClass().getResource("match1.xls");
			
			InputStream in = getClass().getResourceAsStream("/match1.xls"); 
			Workbook workbook = Workbook.getWorkbook(in);
			Sheet sheet = workbook.getSheet(0);
			for(int i = 0; i < sheet.getRows(); i++){
				 Cell timeCell = sheet.getCell(1, i);
				 Cell keyCell = sheet.getCell(4, i);
				 Cell typeCell = sheet.getCell(6, i);
				// System.out.println(i+"   "+timeCell.getContents() +"    " + keyCell.getContents() + "   "+typeCell.getContents()); 
				 list.add(new EventObj(timeCell.getContents(),keyCell.getContents(),typeCell.getContents()));
			}
		} catch (BiffException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

}